package com.mo.controller;

import com.mo.command.ServiceCommand;

import com.mo.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
//返回值类型不一致无法调用,@HystrixCommand不需要再指定
@DefaultProperties(defaultFallback = "defaultFallBack")
public class CommandController {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/find/{id}")
    @HystrixCommand//(fallbackMethod = "hystrixMethod")//参数，返回值一致
    public Map find(@PathVariable Long id){
        Map map = new HashMap();
        map = restTemplate.getForObject("http://provide/pro", Map.class);
        User user = new User();
        user.setName(map.toString());
        return map;
    }

    /**
     * 降级方法
     */
    public Map hystrixMethod(Long id){
        Map map = new HashMap();
        map.put("11","降级方法");
        User user = new User();
        user.setName(map.toString());
        return map;
    }

    /**
     * 所有接口返回值需要一样
     * @return
     */
    public Map defaultFallBack(){
        Map map = new HashMap();
        map.put("22","公共的降级方法");
        User user = new User();
        user.setName(map.toString());
        return map;
    }

}
